1286A - Garland - CodeForces Solution


dp greedy sortings *1800

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define LL long long
using namespace std;
int main()
{
  int n;
  scanf("%d",&n);
  int ganjil=(n+1)/2;
  int genap=(n/2);
  int arr[n+5];
  for(int a=1;a<=n;a++)
  {
    scanf("%d",&arr[a]);

  }
  int dp[n+4][ganjil+5][genap+5][2];
  for(int a=0;a<=n;a++)
    for(int b=0;b<=ganjil;b++)
      for(int c=0;c<=genap;c++)
        for(int d=0;d<=1;d++)
          dp[a][b][c][d]=1e9;
  dp[0][0][0][0]=0;
  dp[0][0][0][1]=0;

  // panjang , sisa ganjil, sisa genap,sebelumnya, sekarang;
  for(int a=1;a<=n;a++)
  {
    for(int b=0;b<=ganjil;b++)
    {
      for(int c=0;c<=genap;c++)
      {
        for(int e=0;e<=1;e++)
        {
            if(b==0 && c==0)
            continue;
            if(b==0 && e==1)
            continue;
            if(c==0 && e==0)
            continue;
            if(arr[a])
            {
              if(arr[a]%2==1 && e==0)
              continue;
              if(arr[a]%2==0 && e==1)
              continue;
            }
            if(e==0)// kalau enya genap
            {

              dp[a][b][c][e]=min(dp[a-1][b][c-1][e],dp[a][b][c][e]);
              dp[a][b][c][e]=min(1+dp[a-1][b][c-1][1],dp[a][b][c][e]);
            }
            if(e==1)// kalau enya ganjil
            {

              dp[a][b][c][e]=min(dp[a-1][b-1][c][e],dp[a][b][c][e]);
              dp[a][b][c][e]=min(1+dp[a-1][b-1][c][0],dp[a][b][c][e]);
            }
            if(a==1 && dp[a][b][c][e]>=0)
            dp[a][b][c][e]=0;
          
        }
        
      }
    }
  }
//  for(int a=0;a<=n;a++)
//    for(int b=0;b<=ganjil;b++)
//      for(int c=0;c<=genap;c++)
//        for(int d=0;d<=1;d++)
//          printf("dp[%d][%d][%d][%d]=%d\n",a,b,c,d,dp[a][b][c][d]);
//  int maks=-1e9;
  if(arr[n])
  {
    if(arr[n]&1)
    printf("%d\n",dp[n][ganjil][genap][1]);
    else
    printf("%d\n",dp[n][ganjil][genap][0]);  
  }
  else
  printf("%d\n",min(dp[n][ganjil][genap][1],dp[n][ganjil][genap][0]));
  
//  printf("%d\n",maks);
}


Comments

Submit
0 Comments
More Questions

1547C - Pair Programming
550A - Two Substrings
797B - Odd sum
1093A - Dice Rolling
1360B - Honest Coach
1399C - Boats Competition
1609C - Complex Market Analysis
1657E - Star MST
1143B - Nirvana
1285A - Mezo Playing Zoma
919B - Perfect Number
894A - QAQ
1551A - Polycarp and Coins
313A - Ilya and Bank Account
1469A - Regular Bracket Sequence
919C - Seat Arrangements
1634A - Reverse and Concatenate
1619C - Wrong Addition
1437A - Marketing Scheme
1473B - String LCM
1374A - Required Remainder
1265E - Beautiful Mirrors
1296A - Array with Odd Sum
1385A - Three Pairwise Maximums
911A - Nearest Minimums
102B - Sum of Digits
707A - Brain's Photos
1331B - Limericks
305B - Continued Fractions
1165B - Polycarp Training